package com.woniu.config;

import com.woniu.entity.RabbitmqConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitmqConfig {


    @Bean
    public Queue checkOrderStatQueue() {
        return new Queue(RabbitmqConst.CHECKORDERSTAT_QUEUE);
    }

    @Bean
    public CustomExchange delayExchange() {
        Map map = new HashMap();
        //指定交换机类型
        map.put("x-delayed-type", "direct");

        /**
         * 参数1：交换机名字
         * 参数2：指定交换机的消息类型（延迟类型）
         * 参数3：是否持久化
         * 参数4：是否自动删除
         * 参数5：其他参数（指定交换机类型）
         */
        return new CustomExchange(RabbitmqConst.DELAY_EXCHANGE, "x-delayed-message",
                true, false, map);
    }

    @Bean
    public Binding checkOrderStatQueueToDelayExchange(Queue checkOrderStatQueue, CustomExchange delayExchange) {
        return BindingBuilder.bind(checkOrderStatQueue).to(delayExchange).with("delay").noargs();
    }

    /**
     * 秒杀订单队列
     */
    @Bean
    public Queue addKillBookQueue() {
        return new Queue(RabbitmqConst.ADDKILLBOOK_QUEUE);
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(RabbitmqConst.TOPIC_EXCHANGE);
    }

    @Bean
    public Binding addKillBookQueueToTopicExchange(Queue addKillBookQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(addKillBookQueue).to(topicExchange).with("killBook");
    }

}
